//
//  WorkOrderViewController.m
//  NewPro
//
//  Created by RYX on 2018/11/22.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "WorkOrderViewController.h"
#import "WorkOrderCell.h"
#import "WorkOrderDetailViewController.h"
#import "HadHandleWorkOrderViewController.h"
#import "WorkOrderStore.h"
#import "WorkOrderList.h"
#import "WorkOrderModel.h"

@interface WorkOrderViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,copy) NSArray *listArr;

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIButton *popBtn;
@property (nonatomic,strong) UIView *bgView;
@end

@implementation WorkOrderViewController

- (UIButton *)popBtn{
    
    if (!_popBtn) {
        
        _popBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth - 110, -100, 100, 44)];
        [_popBtn setImage:[UIImage imageNamed:@"img_hadHandle"] forState:UIControlStateNormal];
        [_popBtn addTarget:self action:@selector(onPopBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _popBtn;
}

- (UIView *)bgView{
    
    if (!_bgView) {
        
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        [_bgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didBgView)]];
    }
    
    return _bgView;
}

- (void)didBgView{
    
    MJWeakSelf
    [UIView animateWithDuration:0.2 animations:^{
        
        weakSelf.popBtn.y = - 100;
        weakSelf.bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        
    } completion:^(BOOL finished) {
        
        [weakSelf.bgView removeFromSuperview];
    }];
    
}

- (void)onRightBtn:(UIButton *)sender{
    
    MJWeakSelf
    sender.selected = !sender.selected;
    if (sender.selected) {
        
        [weakSelf.view insertSubview:weakSelf.bgView belowSubview:weakSelf.popBtn];
        [UIView animateWithDuration:0.25 animations:^{
            
            weakSelf.popBtn.y = 2;
            weakSelf.bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        }];
    }else{
        
        [weakSelf didBgView];
    }
}

- (void)setNavBar{
    
    [self setNavBarWithTitle:@"工单处理"];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //修改按钮向左偏移10 point
    [rightBtn setFrame:CGRectMake(10.0, 0.0, 44.0, 44.0)];
    [rightBtn addTarget:self action:@selector(onRightBtn:) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitle:@"+" forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:25];
    //修改方法
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 44.0, 44.0)];
    [view addSubview:rightBtn];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    
    self.navigationItem.rightBarButtonItem = rightItem;
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"POSTWORKORDERSUCCESS" object:nil];
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setUI{
    
    [self setNavBar];

    [self.view addSubview:self.tableView];

    [self.view insertSubview:self.popBtn belowSubview:self.navigationController.navigationBar];

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.left.right.bottom.equalTo(self.view);

    }];

    [self.tableView.mj_header beginRefreshing];
}
- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionHeaderHeight = 9.5;
        _tableView.sectionFooterHeight = 0.5;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        
        MJWeakSelf
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [weakSelf refresh];
        }];
        
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;
    }
    
    return _tableView;
}

- (void)refresh{
    
    WorkOrderStore *store = [[WorkOrderStore alloc] init];
    
    NSString *customId = [UserDefaultsManager sharedInstance].user_id;
    
    MJWeakSelf
    [store getWorkOrderListWithCustomId:customId Success:^(NSArray *listArr) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        weakSelf.listArr = listArr;
        [weakSelf.tableView reloadData];
        
    } Failure:^(NSError *error) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [WorkOrderCell getHeight];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.listArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WorkOrderCell *cell = [WorkOrderCell tempWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.listArr[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WorkOrderModel *model = self.listArr[indexPath.section];
    
    if ([model.workOrderState isEqualToString:@"2"]) {
        
        return;
    }
    
    [WorkOrderList sharedInstance].selectedModel = model;
    
    WorkOrderDetailViewController *VC = [[WorkOrderDetailViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
    
}
- (void)onPopBtn:(UIButton *)sender {
    
    [self didBgView];
    HadHandleWorkOrderViewController *VC = [[HadHandleWorkOrderViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

@end
